<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-input v-model="search_word" size="mini" placeholder="请输入搜索歌手姓名" class="handle-input"></el-input>
        <el-button type="primary" @click="dialogVisible = true">添加歌手</el-button>
        <el-button type="primary" @click="delAllButton">批量删除</el-button>
      </div>
      <el-dialog title="添加歌手" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="ruleForm.sex">
              <el-radio :label="0">女</el-radio>
              <el-radio :label="1">男</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="生日" required>
            <el-col :span="11">
              <el-form-item prop="birth">
                <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.birth" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="地区" prop="location">
            <el-input v-model="ruleForm.location"></el-input>
          </el-form-item>
          <el-form-item label="自我介绍" prop="introduction">
            <el-input type="textarea" v-model="ruleForm.introduction"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addSinger">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="修改歌手" :visible.sync="editVisible" width="30%" :before-close="handleClose">
        <el-form :model="editForm" :rules="rules" ref="editForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="editForm.name"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="editForm.sex">
              <el-radio :label="0">女</el-radio>
              <el-radio :label="1">男</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="生日" required>
            <el-col :span="11">
              <el-form-item prop="birth">
                <el-date-picker value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" placeholder="选择日期" v-model="editForm.birth"
                  style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="地区" prop="location">
            <el-input v-model="editForm.location"></el-input>
          </el-form-item>
          <el-form-item label="自我介绍" prop="introduction">
            <el-input type="textarea" v-model="editForm.introduction"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" @click="editSinger">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="删除歌手" :visible.sync="delVisible" width="30%" :before-close="handleClose">
        <span>删除之后不可恢复，确定删除？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="delVisible = false">取 消</el-button>
          <el-button type="primary" @click="delSinger()">确 定</el-button>
        </span>
      </el-dialog>


      <div>
        <el-table :data="this.data" ref="multipleTable" size="mini" border style="width: 100%" height="630px"
        @selection-change="delAll" >

          <el-table-column type="selection"></el-table-column>
          <el-table-column label="头像" width="180" align="center">
            <template slot-scope="scope">
              <div class="singer-image">
                <img :src="getUrl(scope.row.pic)" alt="" style="width: 100%;" />
              </div>
              <el-upload class="upload-demo" :action="uploadUrl(scope.row.id)" :show-file-list="false" :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <el-button type="success," size="mini">更新图片</el-button>
              </el-upload>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="姓名" width="120" align="center">
          </el-table-column>
          <el-table-column prop="sex" label="性别" width="80" align="center">
            <template slot-scope="scope">
              <span v-text="changeSex(scope.row.sex)"></span>
            </template>
          </el-table-column>
          <el-table-column label="生日" width="110" align="center">
            <template slot-scope="scope">
              <div>{{attachBirth(scope.row.birth) }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="location" label="地区" width="90" align="center"></el-table-column>
          <el-table-column label="简介" align="center" width="450px">
            <template slot-scope="scope" align="center">
              <p style="height: 100px; overflow: scroll">{{ scope.row.introduction }}</p>
            </template>
          </el-table-column>
          <el-table-column label="歌曲管理" align="center" width="100px">
            <template slot-scope="scope" align="center">
              <el-button size="mini" type="text" @click="SongManage(scope.row.id,scope.row.name)">歌曲管理</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope" align="center">
              <el-button type="primary" size="mini" @click="editSingerButton(scope.row)">修改</el-button>
              <el-button type="primary" size="mini" @click="delSingerButton(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination @current-change="handleCurrentChange" background layout="total, prev, pager, next"
            :current-page="currentPage" :page-size="pageSize" :total="tableData.length">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    mixin
  } from '../mixins/index.js';

  import moment from 'moment'
  export default {
    mixins: [mixin],
    computed: {
      // 计算当前表格中的数据
      data() {
        return this.tableData.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
      }
    },
    data() {
      return {
        multipleSelect:[],
        inx: -1,
        currentPage: 1,
        pageSize: 5,
        tableData: [],
        tempData: [],
        search_word: '',
        dialogVisible: false,
        editVisible: false,
        delVisible: false,
        ruleForm: {
          name: '',
          sex: '',
          birth: '',
          pic: '',
          location: '',
          introduction: '',
        },
        editForm: {
          name: '',
          sex: '',
          birth: '',
          location: '',
          introduction: '',
          id: ''
        },
        rules: {
          name: [{
            required: true,
            message: '请输入活动名称',
            trigger: 'blur'
          }],
          sex: [{
            required: true,
            message: '请选择性别',
            trigger: 'blur'
          }],
          birth: [{
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'blur'
          }],
          location: [{
            required: true,
            message: '请填写地区',
            trigger: 'blur'
          }],
          introduction: [{
            required: true,
            message: '请填写自我介绍',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      SongManage(singerId,singerName){
        this.$router.push({path:"Song" ,query:{"singerId":singerId,"singerName":singerName}});
      },
      delAll(all){
        this.multipleSelect=all;
      },
      delAllButton(){
        for(let item of this.multipleSelect){
          this.inx = item.id;
          this.delSinger();
        }
      },
      delSinger() {
        this.axios({
          method: "get",
          url: "http://localhost:8888/music/singer/delete?id="+this.inx,
          withCredentials: true,
        }).then((res) => {
              if (res.data.code == 1) {
                this.$notify({
                  title: res.data.description,
                  message: this.editForm.name + "删除成功",
                  type: 'success'
                });
                this.delVisible = false;
                this.getData();
              }
              if (res.data.code == 0) {
                this.$notify.error({
                  title: res.data.description,
                  message: this.editForm.name + "删除失败",
                });
              }
            })
      },
      delSingerButton(id) {
        this.inx = id;
        this.delVisible = true;
      },
      editSinger() {
        let data = new FormData();
        data.append("id", this.editForm.id);
        data.append("name", this.editForm.name);
        data.append("sex", this.editForm.sex);
        data.append("birth", /* moment(this.editForm.birth).format('YYYY-MM-DD HH:mm:ss') */ this.editForm.birth);
        data.append("introduction", this.editForm.introduction);
        data.append("location", this.editForm.location);
        data.append("pic", this.editForm.pic);
        var jsonData = {};
        data.forEach((value, key) => jsonData[key] = value);
        alert(jsonData.birth);
        this.axios({
          method: "post",
          url: "http://localhost:8888/music/singer/update",
          withCredentials: true,
          data: jsonData
        }).then((res) => {
          if (res.data.code == 1) {
            this.$notify({
              title: res.data.description,
              message: this.editForm.name + "修改成功",
              type: 'success'
            });
            this.editVisible = false;
            this.getData();
          }
          if (res.data.code == 0) {
            this.$notify.error({
              title: res.data.description,
              message: this.editForm.name + "修改失败",
            });
          }
        })


      },
      editSingerButton(row) {
        this.editVisible = true;
        this.editForm.name = row.name;
        this.editForm.id = row.id;
        this.editForm.pic = row.pic;
        this.editForm.location = row.location;
        this.editForm.introduction = row.introduction;
        this.editForm.sex = row.sex;
        this.editForm.birth = row.birth;
      },
      handleCurrentChange(val) {
        this.currentPage = val
      },
      uploadUrl(id) {
        return this.$store.state.HOST + '/singer/updateSingerPic?id=' + id
      },
      handleClose(done) {

          this.$confirm('确认关闭？')
            .then(_ => {
              done();
            })
            .catch(_ => {});

      },
      addSinger() {

        let data = new FormData();
        data.append("name", this.ruleForm.name);
        data.append("sex", this.ruleForm.sex);
        data.append("birth", this.ruleForm.birth);
        data.append("introduction", this.ruleForm.introduction);
        data.append("location", this.ruleForm.location);
        this.axios({
          method: "post",
          url: "http://localhost:8888/music/singer/add",
          withCredentials: true,
          data: data
        }).then((res) => {
          if (res.data.code == 1) {
            this.$notify({
              title: res.data.description,
              message: this.ruleForm.name + "添加成功",
              type: 'success'
            });
            this.dialogVisible = false;
            this.getData();

          }
          if (res.data.code == 0) {
            this.$notify.error({
              title: res.data.description,
              message: this.ruleForm.name + "添加失败",
            });
          }
        })

      },
      getData() {
        this.tempData = [];
        this.tableData = [];
        this.axios({
          method: "get",
          url: "http://localhost:8888/music/singer/findAll",
          withCredentials: true,
        }).then((res) => {
          if (res.data.code == 1) {
            this.tableData = res.data.data;
            this.tempData = res.data.data;
          }
          if (res.data.code == 0) {
            this.$notify.error({
              title: res.data.description,
              message: "查询失败",
            });
          }
        })
      }
    },
    watch: {
      search_word: function() {
        if (this.search_word == '') {
          this.tableData = this.tempData;
        } else {
          this.tableData = [];
          this.currentPage = 1;
          for (let item of this.tempData) {
            if (item.name.includes(this.search_word)) {
              alert(item.name)
              this.tableData.push(item);
            }
          }

        }
      },

    },
    created() {
      this.getData();
    }
  };
</script>

<style>
  .handle-box {
    margin-bottom: 5px;
  }

  .singer-img {
    width: 100%;
    height: 80px;
    border-radius: 5px;
    margin-bottom: 5px;
    overflow: hidden;
  }

  .handle-input {
    width: 300px;
    display: inline-block;
  }

  .pagination {
    display: flex;
    justify-content: center;
  }
</style>
